
cnt=0;
eye_traces_mean=[];
eye_pos_dens_mean1=[];
eye_pos_dens_mean2=[];
l_win=5000;
r_win=5000;
cnt1=0;
cnt2=0;
total_frames=0;
total_time=0;

for exp_id=[1:8 10:33]
    if AV_data(exp_id).ACode==1
        eye=[];
        eye(1,:)=AV_data(exp_id).pupil_pos(1,:)-median(AV_data(exp_id).pupil_pos(1,:))+100;
        eye(2,:)=AV_data(exp_id).pupil_pos(2,:)-median(AV_data(exp_id).pupil_pos(2,:))+100;
        
        total_frames=total_frames+size(eye,2);
        total_time=total_time+length(AV_data(exp_id).PS);
        
        cnt=cnt+1;
        eye_pos_dens=zeros(200,200);
        
        if length(AV_data(exp_id).shutter_open)>3
            PB_id=ones(length(AV_data(exp_id).PS),1);
            PB_id(1:AV_data(exp_id).shutter_open(2))=0;
            PB_id=PB_id(round(AV_data(exp_id).iframe_times))';
        else
            PB_id=(AV_data(exp_id).PS>0.2);
            PB_id(1:AV_data(exp_id).shutter_open(2))=0;
            PB_id=PB_id(round(AV_data(exp_id).iframe_times));
        end
        
        r_id=(AV_data(exp_id).velP_ind(round(AV_data(exp_id).iframe_times/10)))';
        f_id=(AV_data(exp_id).velM_ind(round(AV_data(exp_id).iframe_times/10)))';
        
        FM_id=AV_data(exp_id).velP_ind&~AV_data(exp_id).velM_ind;
        FM_id=(FM_id(round(AV_data(exp_id).iframe_times/10)))';
        
        PS_id=AV_data(exp_id).PS(round(AV_data(exp_id).iframe_times))>0.7;
        
        curr_id=f_id;
        
        for ind=find(r_id)
            eye_pos_dens(eye(1,ind),eye(2,ind))=eye_pos_dens(eye(1,ind),eye(2,ind))+1;
            cnt1=cnt1+1;
        end
%         figure;
%         subplot(121)
%         imagesc(eye_pos_dens)
%         colorbar
%         xlim([80 120])
%         ylim([80 120])
        
        eye_pos_dens_mean1(:,:,cnt)=eye_pos_dens;
        
        eye_pos_dens=zeros(200,200);
        for ind=find(~r_id)
            eye_pos_dens(eye(1,ind),eye(2,ind))=eye_pos_dens(eye(1,ind),eye(2,ind))+1;
            cnt2=cnt2+1;
        end
%         subplot(122)
%         imagesc(eye_pos_dens)
%         colorbar
%         xlim([80 120])
%         ylim([80 120])
        
        eye_pos_dens_mean2(:,:,cnt)=eye_pos_dens;
        
        running_onsets=find(diff(smooth(AV_data(exp_id).velP_ind,100)>0.5)==1)*10;
        
        eye_ms=[];
        [eye_ms(1,:)]=map_ROI_act_to_aux_data(eye(1,:),AV_data(exp_id).iframe_times,length(AV_data(exp_id).PS));
        [eye_ms(2,:)]=map_ROI_act_to_aux_data(eye(2,:),AV_data(exp_id).iframe_times,length(AV_data(exp_id).PS));
        [eye_ms(3,:)]=map_ROI_act_to_aux_data(AV_data(exp_id).pupil_diam,AV_data(exp_id).iframe_times,length(AV_data(exp_id).PS));
        

        
        running_onsets=running_onsets(running_onsets>l_win);
        running_onsets=running_onsets(running_onsets<length(eye_ms)-r_win-1);
        
        eye_traces=zeros(l_win+r_win+1,length(running_onsets));
        for ind=1:length(running_onsets)
            eye_traces(:,ind)=eye_ms(1,running_onsets(ind)-l_win:running_onsets(ind)+r_win);
        end
        
        eye_traces_mean(:,cnt)=mean(eye_traces')-median(eye_traces(:));
%         figure;plot(mean(eye_traces'))
    end
    
end

x_scale5deg=4.7;%in pix
y_scale5deg=5.4;%in pix

figure(1);clf; colormap gray
subplot(221)
imagesc(sum(eye_pos_dens_mean1,3)'/sum(eye_pos_dens_mean1(:)))
xlim([80 120])
ylim([80 120])
set(gca,'clim',[0 0.012])
set(gca,'xtick',[-2:2:2]*x_scale5deg+100,'xticklabel',[-10:10:10])
set(gca,'ytick',[-2:2:2]*y_scale5deg+100,'yticklabel',[-10:10:10])
title(num2str(cnt1/total_frames))

subplot(223)
imagesc(sum(eye_pos_dens_mean2,3)'/sum(eye_pos_dens_mean2(:)))
xlim([80 120])
ylim([80 120])
set(gca,'clim',[0 0.012])
set(gca,'xtick',[-2:2:2]*x_scale5deg+100,'xticklabel',[-10:10:10])
set(gca,'ytick',[-2:2:2]*y_scale5deg+100,'yticklabel',[-10:10:10])
title(num2str(cnt2/total_frames))

subplot(222)
hold on
plot(sum(sum(eye_pos_dens_mean1,3))/sum(eye_pos_dens_mean1(:)),'linewidth',2)
plot(sum(sum(eye_pos_dens_mean2,3))/sum(eye_pos_dens_mean2(:)),'r','linewidth',2)
xlim([-3 3]*y_scale5deg+100)
set(gca,'xtick',[-2:2:2]*y_scale5deg+100,'xticklabel',[-10:10:10])

subplot(224)
hold on
plot(sum(sum(eye_pos_dens_mean1,3)')/sum(eye_pos_dens_mean1(:)),'linewidth',2)
plot(sum(sum(eye_pos_dens_mean2,3)')/sum(eye_pos_dens_mean2(:)),'r','linewidth',2)
xlim([-3 3]*x_scale5deg+100)
set(gca,'xtick',[-2:2:2]*x_scale5deg+100,'xticklabel',[-10:10:10])


set(gcf,'position',[-1000 300 500 450])


% 
% for ind=1:size(eye_traces_mean,2)
%     eye_traces_mean(:,ind)=eye_traces_mean(:,ind)-mean(eye_traces_mean(1:l_win,ind));
% end
% figure;hold on
% plot(mean(eye_traces_mean'),'k')
% plot(mean(eye_traces_mean')+std(eye_traces_mean')/sqrt(size(eye_traces_mean,2)),':k')
% plot(mean(eye_traces_mean')-std(eye_traces_mean')/sqrt(size(eye_traces_mean,2)),':k')
% axis tight

% 
